package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.dto.ShoppingCartDto;
import com.itheima.dto.SubShoppingDto;
import com.itheima.service.ShoppingCartService;
import com.itheima.vo.ShoppingCartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
@Autowired
private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public CommonResult add(@RequestBody ShoppingCartDto shoppingCartDto) {
        boolean res = shoppingCartService.add(shoppingCartDto);
        if (!res){
        return CommonResult.error("添加失败");
        }
        return CommonResult.success("添加成功");
    }
    @GetMapping("list")
    public CommonResult getList(){
        List<ShoppingCartVo> listVo = shoppingCartService.getList();

        return CommonResult.success(listVo);
    }
    @DeleteMapping("/clean")
    public CommonResult clean(){
        boolean clean = shoppingCartService.clean();
        if (!clean){
            return CommonResult.error("清空购物车失败");
        }
        return CommonResult.success("清空购物车成功");
    }
    @PostMapping("/sub")
    public CommonResult sub(@RequestBody SubShoppingDto subShoppingDto){
        boolean sub = shoppingCartService.sub(subShoppingDto);
        if (!sub){
            return  CommonResult.error("删除失败");
        }
        return CommonResult.success("删除成功");
    }

    }
